<?php
/**
 * http相关函数
 * @author xnny
 * @version 
 */

/**
 * http相关函数
 * @author wangtao
 *
 */
class http
{
	/**
	 * get传值
	 * @access public
	 * @param string $str		URL中的字段名
	 * 
	 * @return string
	 */
	static function get($str)
	{
		if (isset($_GET[$str]))
		{
			return $str = trim($_GET[$str]);
		}
		else
		{
			return false;
		}
	}
	
	/**
 	* 发送一次POST请求
 	* @param array		$data	发送的数据
 	* @param string	$url	 接收数据的URL
 	*/
	static function sendPOST($data, $url)
	{
		$ch = curl_init();
		//print_r($data);echo $url;exit;
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); //不直接输出字符
		curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器
		curl_setopt($ch, CURLOPT_POST, 1); // 发送一个常规的Post请求
		curl_setopt($ch, CURLOPT_HEADER, 0);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $data); // Post提交的数据包
		
		$str = curl_exec($ch);
		return $str;
	}
	
	static function showAllPage($countNum, $page)
	{
		$page_string = '';
		if ($page == $countNum)
		{
			$page_string .= '';
		}
		else
		{
			$page_string .= '<a href="/showAll/index' .'">首页</a>';
			$page_string .= '<a href="/showAll/' . ($page + 30) . '">上一页</a>';
		}
		
		if ($page == 0)
		{
			$page_string .= '';
		}
		else
		{
			$page_string .= '<a href="/showAll/' . ($page - 30) . '">下一页</a>';
			$page_string .= '<a href="/showAll/0' .'">尾页</a>';
		}
		
		return $page_string;
	}
	
	/**
	 * 哥是分页
	 * @access	public
	 * @param	int			$conutNum		记录数总量
	 * @param	string		$url			传入的URL
	 * @param	int			$PageSize		每页记录数，默认为10
	 * 
	 * @return	string		$page_string
	 */
	static function rssPageF($conutNum, $start, $url, $PageSize = 10)
	{
		// 翻页链接
		$page_string = '';
		if (wt_math::gTO0($conutNum) > $start)
		{
			$page_string .= '<a href="' . $url . '/index">第一页</a>';
			if (wt_math::gTO0($conutNum) != ($start + 10))
			{
				$page_string .= '<a href="' . $url . '/' . ($start + 10) . '">上一页</a>';
			}
		}
		if (wt_math::gTO0($conutNum) >= $start && ($start-$PageSize) >= 0)
		{
			$page_string .= '<a href="' . $url .'/' . ($start - 10) . '">下一页</a>
			<a href="' . $url . '/0' .'">尾页</a>';
		}
		return $page_string;
	}

	static function searchPageF($conutNum, $start, $url, $PageSize = 10)
	{
		$page_string = '';
		// 翻页链接
		if ($conutNum > $start)
		{
			$page_string .= '<a href="' . $url . '">第一页</a>';
			if (wt_math::gTO0($conutNum) != ($start + 10))
			{
				$page_string .= '<a href="' . $url . '&st=' . ($start + 10) . '">上一页</a>';
			}
		}
		if ($conutNum >= $start && ($start-$PageSize) > 0)
		{
			$page_string .= '<a href="' . $url . '&st=' . ($start - 10) . '">下一页</a>
			<a href="' . $url . '/0' .'">尾页</a>';
		}
		return $page_string;
	}
	static function authorPageF($conutNum, $start, $url, $PageSize = 10)
	{
		$page_string = '';
		// 翻页链接
		if ($conutNum > $start)
		{
			$page_string .= '<a href="' . $url . '">第一页</a>';
			if (wt_math::gTO0($conutNum) != ($start + 10))
			{
				$page_string .= '<a href="' . $url . '/' . ($start + 10) . '">下一页</a>';
			}
		}
		if ($conutNum >= $start && ($start-$PageSize) > 0)
		{
			$page_string .= '<a href="' . $url . '/' . ($start - 10) . '">上一页</a>
			<a href="' . $url . '/0' .'">尾页</a>';
		}
		return $page_string;
	}
	static function onePageF($first, $final, $page = 0)
	{
		$page_string = '';
		if ($page == $final)
		{
			$page_string .= '';
		}
		else
		{
			$page_string .= '<a href="/readOne/' . $final . '">最新</a>
			<a href="/readOne/' . ($page + 1) . '">上一篇</a>';
		}
		
		if ($page == $first)
		{
			$page_string .= '';
		}
		else
		{
			$page_string .= '<a href="/readOne/' . ($page - 1) . '">下一篇</a>
			<a href="/readOne/' . $first .'">尾页</a>';
		}
		return $page_string;
	}
	
	/**
	 * 上页下页，在本博客内
	 * @param int $pid		日志ID
	 * @param int $first
	 * @param int $final
	 */
	static function onpageOF($pid, $first, $final)
	{
		$bid = self::toBid($pid);
		$num = self::toNum($bid, $pid);
		$nextPid	= self::toNextPid($bid, $num);
		$previous	= self::toPrevious($bid, $num);
		$page_string = '';
		$page_string .= '<a href="/readOne/' . $final . '">最新</a>
		<a href="/readOne/' . $previous . '">上一篇</a>';
		$page_string .= '<a href="/readOne/' . $nextPid . '">下一篇</a>
			<a href="/readOne/' . $first .'">尾页</a>';
		return $page_string;
	}
	
	/**
	 * 得到当前文件的序列号
	 * @param int $bid	博客ID
	 * @param int $pid	日志ID
	 */
	static function toNum($bid, $pid)
	{
		$sql = 'SELECT `num` FROM `bid_pid_num` WHERE `pid` =' . $pid . ' AND `bid` =' . $bid . ' LIMIT 0 , 1';
		$db = mysqlDB::con();
		$num = $db->query2one($sql);
		return $num;
	}
	
	static function toBid($pid)
	{
		$sql = 'SELECT `bid` FROM `bid_pid_num` WHERE `pid` =' . $pid . ' LIMIT 0,1';
		$db = mysqlDB::con();
		$bid = $db->query2one($sql);
		return $bid;
	}

	/**
	 * 得到下一文章ID
	 * @param int $bid	博客ID
	 * @param int $num	日志ID
	 */
	static function toNextPid($bid, $num)
	{
		$num = $num + 1;
		$sql = 'SELECT `pid` FROM `bid_pid_num` WHERE `bid` =' . $bid . ' AND `num` =' . $num . ' LIMIT 0 , 1';
		$db = mysqlDB::con();
		$pid = $db->query2one($sql);
		if (empty($pid)) {
			return $num + 1;
		}
		else {
			return $pid;
		}
	}

	/**
	 * 得到上一文章ID
	 * @param int $bid	博客ID
	 * @param int $num	日志ID
	 */
	static function toPrevious($bid, $num)
	{
		$num = $num - 1;
		$sql = 'SELECT `pid` FROM `bid_pid_num` WHERE `bid` =' . $bid . ' AND `num` =' . $num . ' LIMIT 0 , 1';
		$db = mysqlDB::con();
		$pid = $db->query2one($sql);
		if (empty($pid)) {
			return $num - 1;
		}
		else {
			return $pid;
		}
	}
	
	
	static function onePageAJAX($first, $final, $page = 0)
	{
		$page_string = '';
		if ($page == $final)
		{
			$page_string .= '';
		}
		else
		{
			$page_string .= '<a class="start" href="/readOne/' . $final . '" name="' . $page . '">最新</a>
			<a class="start" href="/readOne/' . ($page + 1) . '" name="' . $page . '">上一篇</a>';
		}
		
		if ($page == $first)
		{
			$page_string .= '';
		}
		else
		{
			$page_string .= '<a class="start" href="/readOne/' . ($page - 1) . '" name="' . $page . '">下一篇</a>
			<a class="start" href="/readOne/' . $first .'" name="' . $page . '">尾页</a>';
		}
		return $page_string;
	}

	/**
 	* 哥是分页
 	* @access	public
 	* @param	int			$conutNum		记录数总量
 	* @param	string		$url			传入的URL
 	* @param	int			$PageSize		每页记录数，默认为10
 	* 
 	* @return	string		$page_string
 	*/
	static function pageF2($conutNum, $url, $PageSize = 10)
	{
		$page = http::get('page');
		if ($page == 1)
		{
			$page = 0;
		}
	

		if ($conutNum)
		{
			//如果总数据量小于$PageSize，那么只有一页
			if ($conutNum < $PageSize)
			{
				$page_count = 1;
			}
   			if ($conutNum % $PageSize)
   			{
   				//取总数据量除以每页数的余数
   				//如果有余数，则页数等于总数据量除以每页数的结果取整再加一
   				$page_count = (int)($conutNum / $PageSize) + 1;           
   			}
   			else
   			{
   				//如果没有余数，则页数等于总数据量除以每页数的结果
   				$page_count = $conutNum / $PageSize;                      
   			}
		}
		else
		{
			$page_count = 0;
		}

		// 翻页链接
		$page_string = '';
	
		if ($page == 1 || $page == 0)
		{
			$page_string .= '第一页|上一页|';
		}
		else
		{
			$page_string .= '<a href="' . $url . 'page=1">第一页</a>|<a href="' . $url . 'page=' . ($page - $PageSize) . '">上一页</a>|';
		}
	
		if ((($page + $PageSize) > $conutNum) || ($page_count == 0))
		{
			$page_string .= '下一页|尾页';
		}
		else
		{
			$end = ($page_count-1) * $PageSize;//页尾数
			$i = 10;
			$page_string .= '<a href="' . $url .'page=' . ($page + $PageSize) .'">下一页</a>|<a href="' . $url . 'page=' . $end .'">尾页</a>';
		}
	
		return $page_string;
		$search_html = filter_input(INPUT_GET, 'search', FILTER_SANITIZE_SPECIAL_CHARS);
	}
	
	static function pager()
	{
		$conutNum = 40;
		$pageSize = 10;
		$url = 'http://www.baidu.com';
		$pageString = '';
		$pageNum = 0;
		
		$pageString .= '<a href="' . $url . 'page=' . $pageNum . '">最后一页</a>';
		$pageString .= '<a href="' . $url . 'page=' . ($pageNum + $pageSize) . '">上一页</a>';
		$pageString .= '<a href="' . $url . 'page=' . ($pageNum - $pageSize) . '">下一页</a>';
		$pageString .= '<a href="' . $url . 'page=' . $conutNum . '">第一页</a>';
	}
}
